package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.Attribute;
import cn.gson.boot.model.pojos.AttributeValue;
import cn.gson.boot.model.pojos.ChanpincongbiaoShuxinzhi;
import cn.gson.boot.model.service.AttributeService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/attribute")
public class AttributeController {
    @Autowired
    AttributeService attributeService;

    /**
     * 规格分页查询
     * @param pageNum
     * @param pageSize
     * @param search
     * @param type
     * @return
     */
    @RequestMapping("/attributeList")
    @ResponseBody
    public Map<String,Object> attributeList(Integer pageNum, Integer pageSize, String search, String type){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        attributeService.selectAttr(search,type);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 新增规格名和规格值
     * @param Attr
     */
    @RequestMapping("/addAttribute")
    @ResponseBody
    public void addAttribute(String Attr){
        System.out.println("xxxxx");
        JSONObject obj = (JSONObject)JSONObject.parse(Attr);
        Attribute attribute = obj.getObject("Attribute",Attribute.class);
        AttributeValue AttributeValue = obj.getObject("AttributeValue",AttributeValue.class);
        attributeService.addAttribute(attribute,AttributeValue);
    }


    /**
     * 新增弹框查询所有规格名
     * @return
     */
    @RequestMapping("/selectAttrName")
    @ResponseBody
    public List<Attribute> selectAttrName(){
        return attributeService.selectAttrName();
    }

    /**
     * 新增弹框根据类别id查询所有规格名
     * @return
     */
    @RequestMapping("/selectAttributeName")
    @ResponseBody
    public List<Attribute> selectAttributeName(Integer categoryId){
        return attributeService.selectAttributeName(categoryId);
    }

    /**
     * 查询需要修改的规格
     * @param attributevId
     * @return
     */
    @RequestMapping("/upAttribute")
    @ResponseBody
    public List<Attribute> upAttribute(Integer attributevId){
        return attributeService.upAttribute(attributevId);
    }

    /**
     * 查询类别下有哪些规格
     * @param categoryId
     * @return
     */
    @RequestMapping("/selectAttribute")
    @ResponseBody
    public List<Attribute> selectAttribute(Integer categoryId){
        return attributeService.selectAttribute(categoryId);
    }


    /**
     * 判断某个规格有没有产品使用
     * @param attributevId
     * @return
     */
    @RequestMapping("/delAttribute")
    @ResponseBody
    public List<ChanpincongbiaoShuxinzhi> selectCp(Integer attributevId){
        System.out.println(attributevId);
        return attributeService.selectCp(attributevId);
    }

    /**
     * 删除规格和规格值
     * @param attributevId
     */
    @RequestMapping("/delAttributeValue")
    @ResponseBody
    public void delAttributeValue(Integer attributevId){
        attributeService.delAttributeValue(attributevId);
    }

    /**
     *删除类别时查询类别是否被使用
     * @param categoryId
     * @return
     */
    @RequestMapping("/selectAttrCategory")
    @ResponseBody
    public List<Attribute> selectAttrCategory(Integer categoryId){
        return attributeService.selectAttrCategory(categoryId);
    }

    /**
     *查询类别下有哪些规格
     * @param categoryId
     * @return
     */
    @RequestMapping("/selectCatAttr")
    @ResponseBody
    public List<Attribute> selectCatAttr(Integer categoryId){
        return attributeService.selectCatAttr(categoryId);
    }

}
